<template>
  <div class="audit-container">
    
    <!-- 搜索和过滤区域 -->
    <div class="filter-section">
      <div class="search-box">
        <i class="fas fa-search"></i>
        <input 
          type="text" 
          placeholder="搜索博客标题、作者或内容..." 
          v-model="searchQuery"
        >
      </div>
      
      <div class="filter-controls">
        <select class="filter-select" v-model="statusFilter">
          <option value="all">所有状态</option>
          <option value="pending">待审核</option>
          <option value="reviewed">已审核</option>
          <option value="rejected">已拒绝</option>
        </select>
        
        <select class="filter-select" v-model="sortOption">
          <option value="newest">最新提交</option>
          <option value="oldest">最早提交</option>
        </select>
      </div>
    </div>
    
    <!-- 博客列表 -->
    <div class="blogs-container">
      <div 
        class="blog-card" 
        v-for="blog in filteredBlogs" 
        :key="blog.id"
      >
        <div class="blog-header">
          <a href="javascript:void(0)" class="blog-title" @click="openPreview(blog)">
            {{ blog.title }}
          </a>
          <span class="status" :class="blog.status">
            {{ statusText[blog.status] }}
          </span>
        </div>
        
        <div class="blog-meta">
          <div class="meta-item">
            <i class="fas fa-user"></i>
            {{ blog.author }}
          </div>
          <div class="meta-item">
            <i class="far fa-calendar-alt"></i>
            {{ blog.date }}
          </div>
        </div>
        
        <p class="excerpt">{{ blog.excerpt }}</p>
        
        <div class="blog-tags">
          <span 
            class="tag" 
            v-for="tag in blog.tags" 
            :key="tag"
          >
            {{ tag }}
          </span>
        </div>
        
        <div class="action-bar">
          <div class="reviewer-info" v-if="blog.status=='pending'">
            <div class="avatar">?</div>
            <span>尚未审核</span>
          </div>
          <div class="reviewer-info" v-else>
            <span>已审核</span>
          </div>
          
          <div class="action-buttons">
            <button 
              class="btn view" 
              @click="openPreview(blog)"
            >
               预览
            </button>
            <button 
              class="btn reject" 
              @click="rejectBlog(blog)" 
              v-if="blog.status !== 'rejected'"
            >
               拒绝
            </button>
            <button 
              class="btn approve" 
              @click="approveBlog(blog)" 
              v-if="blog.status !== 'reviewed'"
            >
               通过
            </button>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 分页控件 -->
    <div class="pagination">
      <button 
        class="pagination-btn" 
        v-for="page in totalPages" 
        :key="page"
        :class="{ active: page === currentPage }"
        @click="currentPage = page"
      >
        {{ page }}
      </button>
    </div>
    
    <!-- 博客预览弹窗 -->
    <div class="modal" v-if="previewBlog" @click.self="previewBlog = null">
      <div class="modal-content">
        <div class="modal-header">
          <h2>{{ previewBlog.title }}</h2>
          <button class="modal-close" @click="previewBlog = null">
            <i class="fas fa-times"></i>
          </button>
        </div>
        <div class="modal-body">
          <div class="blog-meta">
            <div class="meta-item">
              <i class="fas fa-user"></i>
              {{ previewBlog.author }}
            </div>
            <div class="meta-item">
              <i class="far fa-calendar-alt"></i>
              {{ previewBlog.date }}
            </div>
            <div class="meta-item">
              <i class="fas fa-tag"></i>
              {{ previewBlog.category }}
            </div>
          </div>
          
          <div class="content-section">
            <div class="content-preview">
              {{previewBlog.content}}
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <button class="btn view" @click="previewBlog = null">
            <i class="fas fa-times"></i> 关闭
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {loadAllPost,changePostStatus} from "../services/commuServ"
import {error} from "../tools/messageBox"

export default {
  name: 'BlogAudit',
  data() {
    return {
      searchQuery: '',
      statusFilter: 'all',
      sortOption: 'newest',
      currentPage: 1,
      blogsPerPage: 5,
      previewBlog: null,
      statusText: {
        pending: '待审核',
        reviewed: '已审核',
        rejected: '已拒绝'
      },
      blogs: [
        {
          id: 1,
          title: 'Vue 3新特性全面解析',
          author: '张明',
          date: '2023-04-15 10:30',
          status: 'pending',
          content:'用户体验设计中，界面设计起着至关重要的作用。本文将介绍设计优雅用户界面的原则与方法...',
        },
        {
          id: 2,
          title: '如何设计优雅的用户界面',
          author: '李思',
          date: '2023-04-14 16:45',
          status: 'reviewed',
          content: '用户体验设计中，界面设计起着至关重要的作用。本文将介绍设计优雅用户界面的原则与方法...',
        }
      ]
    };
  },
  mounted(){
    this.initData();
  },
  activated() {
    this.initData();
  }, 
  computed: {
    filteredBlogs() {
      let filtered = this.blogs;
      
      // 搜索过滤
      if (this.searchQuery) {
        const query = this.searchQuery.toLowerCase();
        filtered = filtered.filter(blog => 
          blog.title.toLowerCase().includes(query) || 
          blog.author.toLowerCase().includes(query) ||
          blog.content.toLowerCase().includes(query)
        );
      }
      
      // 状态过滤
      if (this.statusFilter !== 'all') {
        filtered = filtered.filter(blog => blog.status === this.statusFilter);
      }
      
      // 排序
      if (this.sortOption === 'newest') {
        filtered.sort((a, b) => new Date(b.date) - new Date(a.date));
      } else {
        filtered.sort((a, b) => new Date(a.date) - new Date(b.date));
      }
      
      // 分页
      const start = (this.currentPage - 1) * this.blogsPerPage;
      return filtered.slice(start, start + this.blogsPerPage);
    },
    totalPages() {
      return Math.ceil(this.blogs.length / this.blogsPerPage);
    }
  },
  methods: {
    async initData(){
        try { 
        const res = await loadAllPost();
        if(res.code != 200) {
          error("获取帖子失败");
          return;
        }
        this.blogs = res.data;
      } catch (err) {
        error("请求失败");
      }
    },
    openPreview(blog) {
      this.previewBlog = blog;
    },
    async approveBlog(blog) {
      if (confirm(`确定要通过博客 "${blog.title}" 吗？`)) {
        try {
          const res=await changePostStatus(blog.id,'reviewed');
        } catch (err) {
          error('操作失败')
        }
        this.initData();
      }
    },
    async rejectBlog(blog) {
      if (confirm(`确定要拒绝博客 "${blog.title}" 吗？`)) {
        try{
          const res=await changePostStatus(blog.id,'rejected');
        }catch(err){
          error("操作失败")
        }
        this.initData();
      }
    }
  }
};
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

.audit-container {
  max-width: 1200px;
  margin: 0 auto;
  background: white;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 82, 204, 0.1);
}

.audit-header {
  background: linear-gradient(120deg, #1e88e5, #0d47a1);
  color: white;
  padding: 28px 32px;
  border-radius: 12px 12px 0 0;
}

.audit-header h1 {
  font-size: 32px;
  font-weight: 600;
  margin-bottom: 8px;
  display: flex;
  align-items: center;
}

.audit-header h1 i {
  margin-right: 15px;
  font-size: 36px;
}

.audit-header p {
  opacity: 0.85;
  font-size: 16px;
}

.filter-section {
  background: #e3f2fd;
  padding: 20px 30px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #d0e5ff;
}

.search-box {
  background: white;
  border-radius: 30px;
  padding: 8px 20px;
  display: flex;
  align-items: center;
  width: 320px;
  box-shadow: 0 2px 8px rgba(25, 118, 210, 0.15);
}

.search-box input {
  border: none;
  outline: none;
  padding: 8px;
  width: 100%;
  font-size: 15px;
  background: transparent;
}

.filter-controls {
  display: flex;
  gap: 15px;
}

select.filter-select {
  background: white;
  border: 1px solid #bbdefb;
  border-radius: 8px;
  padding: 10px 15px;
  color: #1565c0;
  font-weight: 500;
  width: 160px;
  outline: none;
  box-shadow: 0 2px 8px rgba(25, 118, 210, 0.1);
  cursor: pointer;
}

.blogs-container {
  padding: 0 20px;
}

.blog-card {
  display: block;
  background: white;
  border-radius: 10px;
  margin: 25px 0;
  padding: 20px 25px;
  box-shadow: 0 4px 15px rgba(0, 82, 204, 0.07);
  border: 1px solid #e3f2fd;
  transition: transform 0.3s, box-shadow 0.3s;
}

.blog-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 6px 20px rgba(25, 118, 210, 0.15);
  border-color: #bbdefb;
}

.blog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.blog-title {
  font-size: 20px;
  font-weight: 600;
  color: #1565c0;
  text-decoration: none;
  display: block;
  cursor: pointer;
}

.blog-title:hover {
  text-decoration: underline;
}

.blog-meta {
  display: flex;
  color: #78909c;
  font-size: 14px;
  margin-bottom: 15px;
  flex-wrap: wrap;
}

.meta-item {
  margin-right: 20px;
  display: flex;
  align-items: center;
  margin-bottom: 5px;
}

.meta-item i {
  margin-right: 6px;
}

.excerpt {
  color: #546e7a;
  line-height: 1.7;
  margin-bottom: 20px;
  font-size: 15px;
}

.blog-tags {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}

.tag {
  background: #e3f2fd;
  color: #1565c0;
  padding: 5px 12px;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 500;
}

.action-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
}

.reviewer-info {
  display: flex;
  align-items: center;
  color: #78909c;
  font-size: 14px;
  margin-right: 15px;
  margin-bottom: 10px;
}

.avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: #bbdefb;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 8px;
  font-size: 12px;
  font-weight: bold;
  color: #0d47a1;
}

.action-buttons {
  display: flex;
  gap: 15px;
  flex-wrap: wrap;
}

.btn {
  padding: 10px 25px;
  border-radius: 6px;
  font-weight: 600;
  cursor: pointer;
  display: felx;
  align-items: center;
  transition: all 0.3s;
  border: none;
  outline: none;
}

.btn i {
  margin-right: 8px;
}

.btn.approve {
  background: linear-gradient(to right, #00c853, #4caf50);
  color: white;
  box-shadow: 0 4px 12px rgba(76, 175, 80, 0.3);
}

.btn.approve:hover {
  background: linear-gradient(to right, #00b248, #43a047);
  transform: translateY(-2px);
}

.btn.reject {
  background: linear-gradient(to right, #ff5252, #f44336);
  color: white;
  box-shadow: 0 4px 12px rgba(244, 67, 54, 0.3);
}

.btn.reject:hover {
  background: linear-gradient(to right, #ff3d3d, #e53935);
  transform: translateY(-2px);
}

.btn.view {
  background: #e3f2fd;
  color: #1565c0;
  box-shadow: 0 4px 12px rgba(25, 118, 210, 0.15);
}

.btn.view:hover {
  background: #bbdefb;
  transform: translateY(-2px);
}

.status {
  padding: 6px 14px;
  border-radius: 20px;
  font-weight: 600;
  font-size: 13px;
  flex-shrink: 0;
}

.status.pending {
  background: #fff3e0;
  color: #ff9800;
}

.status.reviewed {
  background: #e8f5e9;
  color: #4caf50;
}

.status.rejected {
  background: #ffebee;
  color: #f44336;
}

.pagination {
  display: flex;
  justify-content: center;
  margin: 30px 0;
}

.pagination-btn {
  background: white;
  width: 36px;
  height: 36px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 4px;
  font-weight: 600;
  color: #1565c0;
  border: 1px solid #bbdefb;
  cursor: pointer;
  transition: all 0.3s;
}

.pagination-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.pagination-btn.active, 
.pagination-btn:hover:not(:disabled) {
  background: #1e88e5;
  color: white;
}

.pagination-btn i {
  font-size: 14px;
}

.modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-content {
  background: white;
  width: 90%;
  max-width: 800px;
  border-radius: 12px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.3);
  overflow: hidden;
  animation: modalOpen 0.3s ease-out;
}

@keyframes modalOpen {
  from { opacity: 0; transform: translateY(-50px); }
  to { opacity: 1; transform: translateY(0); }
}

.modal-header {
  background: #f5f9ff;
  color: black;
  padding: 20px;
  position: relative;
}

.modal-header h2 {
  font-size: 24px;
  padding-right: 40px;
}

.modal-close {
  position: absolute;
  top: 20px;
  right: 20px;
  background: transparent;
  border: none;
  color: white;
  font-size: 20px;
  cursor: pointer;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s;
}

.modal-close:hover {
  background: rgba(255, 255, 255, 0.2);
}

.modal-body {
  padding: 25px;
  max-height: 70vh;
  overflow-y: auto;
}

.content-section {
  margin-bottom: 25px;
}

.content-section h3 {
  color: #1565c0;
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid #e3f2fd;
}

.content-preview {
  background: #f5f9ff;
  padding: 20px;
  border-radius: 8px;
  line-height: 1.8;
}

.modal-footer {
  padding: 15px 25px;
  background: #f5f9ff;
  text-align: center;
}

@media (max-width: 768px) {
  .filter-section {
    flex-direction: column;
    gap: 15px;
    align-items: flex-start;
  }
  
  .search-box {
    width: 100%;
  }
  
  .filter-controls {
    width: 100%;
    justify-content: space-between;
  }
  
  .stats-section {
    flex-wrap: wrap;
    gap: 15px;
  }
  
  .stat-card {
    min-width: calc(50% - 15px);
  }
  
  .action-bar {
    flex-direction: column;
    align-items: flex-start;
    gap: 15px;
  }
  
  .modal-content {
    width: 95%;
  }
}

@media (max-width: 480px) {
  .stat-card {
    min-width: 100%;
  }
  
  .filter-controls {
    flex-direction: column;
    gap: 10px;
  }
  
  .filter-select {
    width: 100%;
  }
}
</style>